﻿import com.dmotiko.general.*;
import com.dmotiko.prune.*;
import flash.display.BitmapData;
import mx.transitions.easing.*;
import mx.transitions.*;
import mx.utils.*;
/**
 * ...
 * @author sminutoli
 */
class FashionGalleryItem extends BaseClip
{
	
	public var mcShadow:MovieClip;	
	private var bitmap:MovieClip;
	private var container:MovieClip;
	private var loader:MovieClipLoader;
	private var btn_listener:Function;
		
	private function initClip():Void {
		
		bShowed = false;
		_alpha = 0;
		_visible = false;
				
		bitmap = createEmptyMovieClip("bitmap", getNextHighestDepth());
		container = createEmptyMovieClip("container", getNextHighestDepth() );
		if ( container.getDepth() > mcShadow.getDepth() ) container.swapDepths( mcShadow );
		loader = new MovieClipLoader();
		loader.addListener(this);
		mcShadow._alpha = 0;
		
		var root:FashionGalleryItem = this;
		bitmap.onRelease = function() {
			root.dispatchEvent( { type: BaseClipEvent.CHANGED, target: root, data: root.data } );
		}
		bitmap.onRollOver = function() {
			root.showShadow();
		}
		bitmap.onRollOut = function() {
			root.hideShadow();
		}
	}
	
	public function show():Void {
		if ( bShowed ) return;
		this._visible = true;
		registerTween("fade", new Tween( this, "_alpha", Regular.easeOut, this._alpha, 100, 0.5, true), false, true );
	}
	public function hide():Void {
		if ( !bShowed ) return;
		registerTween("fade", new Tween( this, "_alpha", Regular.easeOut, this._alpha, 0, 0.5, true), false, true );
	}
	
	
	private function showShadow():Void {
		registerTween( "fadeShadow", new Tween( mcShadow, "_alpha", Regular.easeOut, mcShadow._alpha, 60, 0.5, true ) );
	}
	
	private function hideShadow():Void {
		registerTween( "fadeShadow", new Tween( mcShadow, "_alpha", Regular.easeOut, mcShadow._alpha, 0, 0.5, true ) );
	}
	
	private function onLoadInit(target:MovieClip):Void {
		
		target._visible = false;
		
        var bmp:BitmapData = new BitmapData(
            target._width, 
            target._height, 
            true);

         bitmap.attachBitmap(
            bmp, 
            1,
            "auto", 
            true);

         bmp.draw(target);
		
		if ( bShowed ) {
			bShowed = false;
			_alpha = 0;
			_visible = false;
			show();
		}
	}
	
	private function refreshData():Void {
		
		loader.loadClip( data.thumb, container );
	}
	
	private function tweenFinished( key:String, tween:Tween):Void {
		if ( key == "fade" && tween.obj._alpha < 100 ) {
			_visible = false;
			hideEnd();
		} else if ( key == "fade" && tween.obj._alpha > 0 )  {
			showEnd();
		}
	}
		
}